﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using FlowerShop.Core.Models;
using BLL.Services;

namespace 花店管理系统.Customer
{
    public partial class Update_Customer : Form
    {
        private readonly Customers _customerService;

        public Update_Customer()
        {
            InitializeComponent();
            _customerService = new Customers();
            this.Load += new System.EventHandler(this.Update_Customer_Load); // 确保事件绑定
        }

        private void Update_Customer_Load(object sender, EventArgs e)
        {
            InitializeCustomerData();
        }

        private void InitializeCustomerData()
        {
            init();
            RefreshCustomerList();
        }

        private void RefreshCustomerList()
        {
            showAll();
        }

        void showAll()
        {
            var customers = _customerService.GetAllCustomers();
            dgvCustomers.DataSource = customers;
        }

        private void dgvCustomers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                LoadSelectedCustomerDetails(e);
            }
        }

        private void LoadSelectedCustomerDetails(DataGridViewCellEventArgs e)
        {
            var customer = (Tpeople)dgvCustomers.Rows[e.RowIndex].DataBoundItem;
            txtId.Text = customer.t_id.ToString();
            txtName.Text = customer.t_name;
            txtPhone.Text = customer.t_mima.ToString();
            txtEmail.Text = customer.email;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ValidateInput())
            {
                MessageBox.Show("客户姓名和电话不能为空");
                return;
            }

            ConfirmUpdate();
        }

        private void ConfirmUpdate()
        {
            DialogResult dr = MessageBox.Show("你确定要修改吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.OK)
            {
                int customerId = int.Parse(txtId.Text);
                var customer = new Tpeople
                {
                    t_id = customerId,
                    t_name = txtName.Text.Trim(),
                    t_mima = int.Parse(txtPhone.Text.Trim()),
                    email = txtEmail.Text.Trim()
                };

                _customerService.UpdateCustomer(customer);



                MessageBox.Show("修改成功！");
                showAll();
            }
        }

        private bool ValidateInput()
        {
            return string.IsNullOrEmpty(txtName.Text) || string.IsNullOrEmpty(txtPhone.Text);
        }

        private void btnCance_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void init()
        {
            // 初始化数据
            showAll();
        }

        private void Update_Customer_Load_1(object sender, EventArgs e)
        {

        }
    }
}
